﻿using NewChillers.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfControlLibrary;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace NewChillers.ViewModels
{
    internal class MainConfigureViewModel : ObservableObject
    {
        // 初始 设备状态 0：停止状态 1：抽真空 2：充氦气 3：排气?
        private int DevStatus = 0;

        // 定义工位属性数组，方便循环访问
        public bool[] WorkstationValveStatus => new bool[]
        {
            WorkstationValveStatus1,
            WorkstationValveStatus2,
            WorkstationValveStatus3,
            WorkstationValveStatus4,
            WorkstationValveStatus5,
            WorkstationValveStatus6
        };

        public MainConfigureViewModel()
        {
            // 1、获取
            Task.Run(async () =>
            {
                while (true)
                {
                    // 真空压力计
                    Pressure1 = DeviceDataStore.VacuumValue_real.ToString();
                    Pressure2 = DeviceDataStore.VacuumValue01_real.ToString();
                    Pressure3 = DeviceDataStore.VacuumValue02_real.ToString();

                    // 压力变送器压力 保留3位小数
                    PressureTrans1 = string.Format("{0:###}", DeviceDataStore.HeliumValue_real);
                    PressureTrans2 = string.Format("{0:###}", DeviceDataStore.HeliumValue01_real);

                    // 设备状态
                    DevStatus = DeviceDataStore.DEV_STATUS;

                    // 获取工位阀门状态
                    WorkstationValveStatus1 = !DeviceDataStore.IsEnableWorkstation_01;
                    WorkstationValveStatus2 = !DeviceDataStore.IsEnableWorkstation_02;
                    WorkstationValveStatus3 = !DeviceDataStore.IsEnableWorkstation_03;
                    WorkstationValveStatus4 = !DeviceDataStore.IsEnableWorkstation_04;
                    WorkstationValveStatus5 = !DeviceDataStore.IsEnableWorkstation_05;
                    WorkstationValveStatus6 = !DeviceDataStore.IsEnableWorkstation_06;

                    // 测试用
                    //WorkstationValveStatus1 = true;
                    //WorkstationValveStatus2 = true;
                    //WorkstationValveStatus3 = true;
                    //WorkstationValveStatus4 = true;
                    //WorkstationValveStatus5 = true;
                    //WorkstationValveStatus6 = true;

                    await Task.Delay(500);
                }
            });

            // 2、处理
            Task.Run(async () =>
            {
                while (true)
                {
                    HandleDevice(DevStatus);

                    await Task.Delay(100);
                }
            });
        }

        // 根据设备当前状态 调用不同方法
        private void HandleDevice(int devStatus)
        {
            ResetDevice(); // 重置

            switch (devStatus)
            {
                case 0:
                    break;
                case 1: // 抽真空
                    DrawDevice();
                    break;
                case 2: // 充氦气
                    PumpDevice();
                    break;
                case 3: // 排气
                    VentDevice();
                    break;
            }

            // 独立处理每个工位的动画
            UpdateWorkstationAnimations(devStatus);
        }

        // 根据工位阀门开启情况 处理动画
        private void UpdateWorkstationAnimations(int devStatus)
        {
            // 为每个工位设置对应的阀门和管道流向
            for (int i = 0; i < 6; i++)
            {
                if (WorkstationValveStatus[i]) // 如果工位启用
                {
                    SetWorkstationAnimation(i, devStatus);
                }
                //else
                //{
                //    SetWorkstationStop(i); // 工位禁用，设置为停止状态
                //}
            }
        }

        private void SetWorkstationAnimation(int i, int devStatus)
        {
            i++; // i是0-5 工位编号是1-6
            switch (i)
            {
                case 1:
                    SetWA1(devStatus);
                    break;
                case 2:
                    SetWA2(devStatus);
                    break;
                case 3:
                    SetWA3(devStatus);
                    break;
                case 4:
                    SetWA4(devStatus);
                    break;
                case 5:
                    SetWA5(devStatus);
                    break;
                case 6:
                    SetWA6(devStatus);
                    break;
            }
        }

        private void SetWA1(int devStatus)
        {
            //WorkstationValveStatus1 = true;
            switch (devStatus)
            {
                case 0: // 停止
                    break;
                case 1: // 抽真空
                    WorkstationFlowDirection1 = 1;
                    WorkstationFlowDirection7 = 1;
                    WorkstationFlowDirection8 = 1;
                    WorkstationFlowDirection9 = 1;
                    WorkstationFlowDirection10 = 1;
                    WorkstationFlowDirection11 = 1;
                    WorkstationFlowDirection12 = 1;
                    break;
                case 2: // 充氦气
                    WorkstationFlowDirection1 = -1;
                    WorkstationFlowDirection7 = -1;
                    WorkstationFlowDirection8 = -1;
                    break;
                case 3: // 排气
                    WorkstationFlowDirection1 = 1;
                    WorkstationFlowDirection7 = 1;
                    WorkstationFlowDirection8 = 1;
                    break;
            }
        }

        private void SetWA2(int devStatus)
        {
            //WorkstationValveStatus2 = true;
            switch (devStatus)
            {
                case 0: // 停止
                    break;
                case 1: // 抽真空
                    WorkstationFlowDirection2 = 1;
                    WorkstationFlowDirection8 = 1;
                    WorkstationFlowDirection9 = 1;
                    WorkstationFlowDirection10 = 1;
                    WorkstationFlowDirection11 = 1;
                    WorkstationFlowDirection12 = 1;
                    break;
                case 2: // 充氦气
                    WorkstationFlowDirection2 = -1;
                    WorkstationFlowDirection8 = -1;
                    break;
                case 3: // 排气
                    WorkstationFlowDirection2 = 1;
                    WorkstationFlowDirection8 = 1;
                    break;
            }
        }

        private void SetWA3(int devStatus)
        {
            //WorkstationValveStatus3 = true;
            switch (devStatus)
            {
                case 0: // 停止
                    break;
                case 1: // 抽真空
                    WorkstationFlowDirection3 = 1;
                    WorkstationFlowDirection10 = 1;
                    WorkstationFlowDirection11 = 1;
                    WorkstationFlowDirection12 = 1;
                    break;
                case 2: // 充氦气
                    WorkstationFlowDirection3 = -1;
                    WorkstationFlowDirection9 = 1;
                    break;
                case 3: // 排气
                    WorkstationFlowDirection3 = 1;
                    WorkstationFlowDirection9 = -1;
                    break;
            }
        }

        private void SetWA4(int devStatus)
        {
            //WorkstationValveStatus4 = true;
            switch (devStatus)
            {
                case 0: // 停止
                    break;
                case 1: // 抽真空
                    WorkstationFlowDirection4 = 1;
                    WorkstationFlowDirection11 = 1;
                    WorkstationFlowDirection12 = 1;
                    break;
                case 2: // 充氦气
                    WorkstationFlowDirection4 = -1;
                    WorkstationFlowDirection9 = 1;
                    WorkstationFlowDirection10 = 1;
                    break;
                case 3: // 排气
                    WorkstationFlowDirection4 = 1;
                    WorkstationFlowDirection9 = -1;
                    WorkstationFlowDirection10 = -1;
                    break;
            }
        }

        private void SetWA5(int devStatus)
        {
            //WorkstationValveStatus5 = true;
            switch (devStatus)
            {
                case 0: // 停止
                    break;
                case 1: // 抽真空
                    WorkstationFlowDirection5 = 1;
                    WorkstationFlowDirection12 = 1;
                    break;
                case 2: // 充氦气
                    WorkstationFlowDirection5 = -1;
                    WorkstationFlowDirection9 = 1;
                    WorkstationFlowDirection10 = 1;
                    WorkstationFlowDirection11 = 1;
                    break;
                case 3: // 排气
                    WorkstationFlowDirection5 = 1;
                    WorkstationFlowDirection9 = -1;
                    WorkstationFlowDirection10 = -1;
                    WorkstationFlowDirection11 = -1;
                    break;
            }
        }

        private void SetWA6(int devStatus)
        {
            //WorkstationValveStatus6 = true;
            switch (devStatus)
            {
                case 0: // 停止
                    break;
                case 1: // 抽真空
                    WorkstationFlowDirection6 = 1;
                    break;
                case 2: // 充氦气
                    WorkstationFlowDirection6 = -1;
                    WorkstationFlowDirection9 = 1;
                    WorkstationFlowDirection10 = 1;
                    WorkstationFlowDirection11 = 1;
                    WorkstationFlowDirection12 = 1;
                    break;
                case 3: // 排气
                    WorkstationFlowDirection6 = 1;
                    WorkstationFlowDirection9 = -1;
                    WorkstationFlowDirection10 = -1;
                    WorkstationFlowDirection11 = -1;
                    WorkstationFlowDirection12 = -1;
                    break;
            }
        }

        //private void SetWorkstationStop(int i)
        //{
        //    i++; // i是0-5 工位编号是1-6
        //    switch (i)
        //    {
        //        case 1:
        //            break;
        //        case 2:
        //            break;
        //        case 3:
        //            break;
        //        case 4:
        //            break;
        //        case 5:
        //            break;
        //        case 6:
        //            break;
        //    }
        //}

        // DirectionFlow
        // 上->下 1 反之 -1
        // 左->右 1 反之 -1

        // 重置设备
        private void ResetDevice()
        {
            //VaccumValveStatus = false; // 真空电磁阀状态
            //VaccumPumpState = false; // 真空泵状态
            //RadioValveStatus = false; // 比例电磁阀状态

            // 泵状态
            PumpState1 = false;
            PumpState2 = false;

            // 阀门状态
            //ValveStatus1 = false;
            ValveStatus4 = false;
            ValveStatus5 = false;
            ValveStatus6 = false;
            ValveStatus7 = false;
            ValveStatus8 = false;

            // 管道流向
            //FlowDirection1 = 0;
            //FlowDirection2 = 0;
            //FlowDirection3 = 0;
            FlowDirection4 = 0;
            FlowDirection5 = 0;
            FlowDirection6 = 0;
            FlowDirection7 = 0;
            FlowDirection8 = 0;

            // 6个阀门及周边管道
            //WorkstationValveStatus1 = false;
            //WorkstationValveStatus2 = false;
            //WorkstationValveStatus3 = false;
            //WorkstationValveStatus4 = false;
            //WorkstationValveStatus5 = false;
            //WorkstationValveStatus6 = false;
            WorkstationFlowDirection1 = 0;
            WorkstationFlowDirection2 = 0;
            WorkstationFlowDirection3 = 0;
            WorkstationFlowDirection4 = 0;
            WorkstationFlowDirection5 = 0;
            WorkstationFlowDirection6 = 0;
            WorkstationFlowDirection7 = 0;
            WorkstationFlowDirection8 = 0;
            WorkstationFlowDirection9 = 0;
            WorkstationFlowDirection10 = 0;
            WorkstationFlowDirection11 = 0;
            WorkstationFlowDirection12 = 0;
        }

        // 抽真空设备
        private void DrawDevice()
        {
            // 泵状态
            PumpState1 = true;
            PumpState2 = true;

            // 阀门状态
            //ValveStatus1 = true;
            ValveStatus4 = true;
            ValveStatus5 = true;
            ValveStatus6 = false;
            ValveStatus7 = false;
            ValveStatus8 = false;

            // 管道流向
            //FlowDirection1 = 1;
            //FlowDirection2 = 1;
            //FlowDirection3 = 1;
            FlowDirection4 = 1;
            FlowDirection5 = -1;
            FlowDirection6 = 0;
            FlowDirection7 = 0;
            FlowDirection8 = 0;
        }

        // 充氦气设备
        private void PumpDevice()
        {
            // 泵状态
            PumpState1 = false;
            PumpState2 = false;

            // 阀门状态
            //ValveStatus1 = true;
            ValveStatus4 = false;
            ValveStatus5 = false;
            ValveStatus6 = true;
            ValveStatus7 = true;
            ValveStatus8 = false;

            // 管道流向
            //FlowDirection1 = -1;
            //FlowDirection2 = -1;
            //FlowDirection3 = 1;
            FlowDirection4 = 0;
            FlowDirection5 = 0;
            FlowDirection6 = 1;
            FlowDirection7 = -1;
            FlowDirection8 = 0;
        }

        // 排气设备
        private void VentDevice()
        {
            // 泵状态
            PumpState1 = false;
            PumpState2 = false;

            // 阀门状态
            //ValveStatus1 = true;
            ValveStatus4 = false;
            ValveStatus5 = false;
            ValveStatus6 = false;
            ValveStatus7 = true;
            ValveStatus8 = true;

            // 管道流向
            //FlowDirection1 = 1;
            //FlowDirection2 = 1;
            //FlowDirection3 = -1;
            FlowDirection4 = 0;
            FlowDirection5 = 0;
            FlowDirection6 = 0;
            FlowDirection7 = 1;
            FlowDirection8 = 1;
        }

        #region 流动方向 属性
        // 1
        //private int _flowDirection1;
        //public int FlowDirection1
        //{
        //    get => _flowDirection1;
        //    set => SetProperty(ref _flowDirection1, value);
        //}

        // 2
        //private int _flowDirection2;
        //public int FlowDirection2
        //{
        //    get => _flowDirection2;
        //    set => SetProperty(ref _flowDirection2, value);
        //}

        // 3
        //private int _flowDirection3;
        //public int FlowDirection3
        //{
        //    get => _flowDirection3;
        //    set => SetProperty(ref _flowDirection3, value);
        //}

        // 4
        private int _flowDirection4;
        public int FlowDirection4
        {
            get => _flowDirection4;
            set => SetProperty(ref _flowDirection4, value);
        }

        // 5
        private int _flowDirection5;
        public int FlowDirection5
        {
            get => _flowDirection5;
            set => SetProperty(ref _flowDirection5, value);
        }

        // 6
        private int _flowDirection6;
        public int FlowDirection6
        {
            get => _flowDirection6;
            set => SetProperty(ref _flowDirection6, value);
        }

        // 7
        private int _flowDirection7;
        public int FlowDirection7
        {
            get => _flowDirection7;
            set => SetProperty(ref _flowDirection7, value);
        }

        // 8
        private int _flowDirection8;
        public int FlowDirection8
        {
            get => _flowDirection8;
            set => SetProperty(ref _flowDirection8, value);
        }
        #endregion

        #region 阀门 属性
        // 1 工位阀 统合6个
        //private bool _valveStatus1;
        //public bool ValveStatus1
        //{
        //    get => _valveStatus1;
        //    set => SetProperty(ref _valveStatus1, value);
        //}

        // 4
        private bool _valveStatus4;
        public bool ValveStatus4
        {
            get => _valveStatus4;
            set => SetProperty(ref _valveStatus4, value);
        }

        // 5
        private bool _valveStatus5;
        public bool ValveStatus5
        {
            get => _valveStatus5;
            set => SetProperty(ref _valveStatus5, value);
        }

        // 6
        private bool _valveStatus6;
        public bool ValveStatus6
        {
            get => _valveStatus6;
            set => SetProperty(ref _valveStatus6, value);
        }

        // 7
        private bool _valveStatus7;
        public bool ValveStatus7
        {
            get => _valveStatus7;
            set => SetProperty(ref _valveStatus7, value);
        }

        // 8
        private bool _valveStatus8;
        public bool ValveStatus8
        {
            get => _valveStatus8;
            set => SetProperty(ref _valveStatus8, value);
        }
        #endregion

        #region 泵 属性
        // 1
        private bool _pumpState1;

        public bool PumpState1
        {
            get => _pumpState1;
            set => SetProperty(ref _pumpState1, value);
        }

        // 2
        private bool _pumpState2;

        public bool PumpState2
        {
            get => _pumpState2;
            set => SetProperty(ref _pumpState2, value);
        }
        #endregion

        #region 真空压力
        private string _pressure1;
        public string Pressure1
        {
            get => _pressure1;
            set => SetProperty(ref _pressure1, value);
        }

        private string _pressure2;
        public string Pressure2
        {
            get => _pressure2;
            set => SetProperty(ref _pressure2, value);
        }

        private string _pressure3;
        public string Pressure3
        {
            get => _pressure3;
            set => SetProperty(ref _pressure3, value);
        }
        #endregion

        #region 压力变送器压力
        private string _pressureTrans1;
        public string PressureTrans1
        {
            get => _pressureTrans1;
            set => SetProperty(ref _pressureTrans1, value);
        }

        private string _pressureTrans2;
        public string PressureTrans2
        {
            get => _pressureTrans2;
            set => SetProperty(ref _pressureTrans2, value);
        }
        #endregion

        #region 工位阀门 原1 属性
        private bool _workstationValveStatus1;
        public bool WorkstationValveStatus1
        {
            get => _workstationValveStatus1;
            set => SetProperty(ref _workstationValveStatus1, value);
        }

        private bool _workstationValveStatus2;
        public bool WorkstationValveStatus2
        {
            get => _workstationValveStatus2;
            set => SetProperty(ref _workstationValveStatus2, value);
        }

        private bool _workstationValveStatus3;
        public bool WorkstationValveStatus3
        {
            get => _workstationValveStatus3;
            set => SetProperty(ref _workstationValveStatus3, value);
        }

        private bool _workstationValveStatus4;
        public bool WorkstationValveStatus4
        {
            get => _workstationValveStatus4;
            set => SetProperty(ref _workstationValveStatus4, value);
        }

        private bool _workstationValveStatus5;
        public bool WorkstationValveStatus5
        {
            get => _workstationValveStatus5;
            set => SetProperty(ref _workstationValveStatus5, value);
        }

        private bool _workstationValveStatus6;
        public bool WorkstationValveStatus6
        {
            get => _workstationValveStatus6;
            set => SetProperty(ref _workstationValveStatus6, value);
        }
        #endregion

        #region 管道流向 原1 2 3 属性
        private int _workstationFlowDirection1;
        public int WorkstationFlowDirection1
        {
            get => _workstationFlowDirection1;
            set => SetProperty(ref _workstationFlowDirection1, value);
        }

        private int _workstationFlowDirection2;
        public int WorkstationFlowDirection2
        {
            get => _workstationFlowDirection2;
            set => SetProperty(ref _workstationFlowDirection2, value);
        }

        private int _workstationFlowDirection3;
        public int WorkstationFlowDirection3
        {
            get => _workstationFlowDirection3;
            set => SetProperty(ref _workstationFlowDirection3, value);
        }

        private int _workstationFlowDirection4;
        public int WorkstationFlowDirection4
        {
            get => _workstationFlowDirection4;
            set => SetProperty(ref _workstationFlowDirection4, value);
        }

        private int _workstationFlowDirection5;
        public int WorkstationFlowDirection5
        {
            get => _workstationFlowDirection5;
            set => SetProperty(ref _workstationFlowDirection5, value);
        }

        private int _workstationFlowDirection6;
        public int WorkstationFlowDirection6
        {
            get => _workstationFlowDirection6;
            set => SetProperty(ref _workstationFlowDirection6, value);
        }

        private int _workstationFlowDirection7;
        public int WorkstationFlowDirection7
        {
            get => _workstationFlowDirection7;
            set => SetProperty(ref _workstationFlowDirection7, value);
        }

        private int _workstationFlowDirection8;
        public int WorkstationFlowDirection8
        {
            get => _workstationFlowDirection8;
            set => SetProperty(ref _workstationFlowDirection8, value);
        }

        private int _workstationFlowDirection9;
        public int WorkstationFlowDirection9
        {
            get => _workstationFlowDirection9;
            set => SetProperty(ref _workstationFlowDirection9, value);
        }

        private int _workstationFlowDirection10;
        public int WorkstationFlowDirection10
        {
            get => _workstationFlowDirection10;
            set => SetProperty(ref _workstationFlowDirection10, value);
        }

        private int _workstationFlowDirection11;
        public int WorkstationFlowDirection11
        {
            get => _workstationFlowDirection11;
            set => SetProperty(ref _workstationFlowDirection11, value);
        }

        private int _workstationFlowDirection12;
        public int WorkstationFlowDirection12
        {
            get => _workstationFlowDirection12;
            set => SetProperty(ref _workstationFlowDirection12, value);
        }
        #endregion

    }
}
